//
//  ViewController.m
//  MJGridView
//
//  Created by Jarvis on 2023/9/8.
//

#import "ViewController.h"
#import "MJGridView.h"
#import <Masonry.h>

@interface ViewController ()
@property (nonatomic, strong) MJGridView *gridView;
@property (nonatomic, assign) NSInteger number;

@property (nonatomic, strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    MJGridView *view = [MJGridView new];
    _gridView = view;
    _number = 10;
    view.backgroundColor = UIColor.purpleColor;
    [view registerView:NSStringFromClass(UIView.class) identifier:NSStringFromClass(UIView.class)];
    view.itemView = ^UIView *(MJGridView *view, NSInteger index) {
        UIView *item = [view dequeueItemWithIdentifier:NSStringFromClass(UIView.class)];
        item.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        return item;
    };
    __weak typeof(self) weak_self = self;
    view.number = ^NSInteger{
        return weak_self.number;
    };
    view.columnSpacing = 10;
    view.rowSpacing = 10;
    view.column = 4;
    view.rowHeight = 50;
    view.insets = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.left.right.equalTo(self.view).inset(20);
//        make.height.mas_equalTo(200);
    }];

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = UIColor.greenColor;
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(buttonAction1) forControlEvents:UIControlEventTouchUpInside];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).inset(20);
        make.top.equalTo(view.mas_bottom).offset(30);
        make.height.mas_equalTo(30);
    }];

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.backgroundColor = UIColor.blueColor;
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(buttonAction2) forControlEvents:UIControlEventTouchUpInside];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).inset(20);
        make.left.equalTo(btn1.mas_right).offset(20);
        make.top.equalTo(view.mas_bottom).offset(30);
        make.width.height.equalTo(btn1);
    }];
}

- (void)buttonAction1 {
    self.number++;
    [_gridView reloadData];
}
- (void)buttonAction2 {
    self.number--;
    [_gridView reloadData];
}

@end
